package com.app.megaprosoft.DB;

import java.sql.*;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class FFUser{
	
    ResultSet rs = null;
    SQLiteDBClass myDb;
    SQLiteDatabase db;
    Context context;
    private static final String TABLE_FFUSER = "_FFUSER";
    
    public ResultSet selectUser(String StrUserName,String StrPassword) throws Exception {
        Connect con = new Connect(); 
        String StrSql = "SELECT [OID] "
        							+ ",[LastModified]"
        							+ ",[ModifyDate]"
        							+ ",[ModifyTime]"
        							+ ",[ModifiedBy]"
        							+ ",[UserModify]"
        							+ ",[CreatedDate]"
        							+ ",[CreateDate]"
        							+ ",[CreateTime]"
        							+ ",[CreatedBy]"
        							+ ",[UserCreate]"
        							+ ",[IsActive]"
        							+ ",[IsDeleted]"
                
                					+ ",[LoginName]"
                					+ ",[UserName]"
                					+ ",[FFUserCode]"
                					+ ",[RightID]"
                					+ ",[UserRightName]"
                					+ ",[DepCode]"
                					+ ",[DepName]"
                					+ ",[UserGroupCode]"
                					+ ",[UserGroupName]"
                					+ ",[Password]"
                					+ ",[PicPath]"
                					+ ",[CreateCode] "
                
                	+ " FROM [dbo].udfFFUser() "
                
                	+ " WHERE [IsDeleted]='0' ";


        if (!StrUserName.equals("")) {
            StrSql += " AND [LoginName] LIKE '" + StrUserName + "' ";
        }
        if (!StrPassword.equals("")) {
            StrSql += " AND [Password] LIKE '" + StrPassword + "' ";
        }
        return con.selectData(StrSql);
    }
    
//    public void closeConnecttion() throws Exception {
//   	 if (conn != null) {
//            conn.close();
//        }
//   }
    
  // ---- SQLite -------//
    public String[][] SelectAll_user(String StrCompanyOID) throws Exception{
		try {
			String arrData[][] = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();
			
			String strSQL = "SELECT  FFUserOID As _id , * FROM " + TABLE_FFUSER;
			if (!StrCompanyOID.equals("")) {
				strSQL += " WHERE [UserGroupCode] LIKE '" + StrCompanyOID
						+ "' ORDER BY [DepCode] DESC";
			}
			Cursor cursor = db.rawQuery(strSQL, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					arrData = new String[cursor.getCount()][cursor
							.getColumnCount()];
					int i = 0;
					do {
						arrData[i][0] = cursor.getString(1);
						arrData[i][1] = cursor.getString(2);
						arrData[i][2] = cursor.getString(3);
						arrData[i][3] = cursor.getString(4);
						arrData[i][4] = cursor.getString(5);
						arrData[i][5] = cursor.getString(6);
						arrData[i][6] = cursor.getString(7);
						arrData[i][7] = cursor.getString(8);
						arrData[i][8] = cursor.getString(9);

						i++;
					} while (cursor.moveToNext());

				}
			}
			cursor.close();
			return arrData;
		} catch (Exception e) {
			return null;
		}
	}
    
	public String[] Select_FFUser(String strLoginName) throws Exception{
		// TODO Auto-generated method stub

		try {
			String arrData[] = null;
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			Cursor cursor = db.query(TABLE_FFUSER, new String[] { "*" },
					"LoginName=?",
					new String[] { String.valueOf(strLoginName) }, null, null,
					null, null);

			if (cursor != null) {
				if (cursor.moveToFirst()) {
					arrData = new String[cursor.getColumnCount()];
					arrData[0] = cursor.getString(0);
					arrData[1] = cursor.getString(1);
					arrData[2] = cursor.getString(2);
					arrData[3] = cursor.getString(3);
					arrData[4] = cursor.getString(4);
					arrData[5] = cursor.getString(5);
					arrData[6] = cursor.getString(6);
					arrData[7] = cursor.getString(7);
					arrData[8] = cursor.getString(8);
				}
			}
			cursor.close();
			db.close();
			return arrData;

		} catch (Exception e) {
			return null;
		}

	}

	public String Select_FFUserName(String FFUserNameOID, String FFUserDefOID) throws Exception{
		// TODO Auto-generated method stub
		try {
			String RetrunName = "";

			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			// String strSQL = "SELECT  FFUserOID As _id , * FROM " +
			// TABLE_FFUSER +" WHERE [FFUserOID] LIKE '" + FFUserNameOID + "' ";

			String strSQL = "SELECT  FFUserOID As _id , * FROM " + TABLE_FFUSER;
			if (!FFUserNameOID.equals("")) {
				strSQL += " WHERE [FFUserOID] LIKE '" + FFUserNameOID + "' ";
			}
			if (!FFUserDefOID.equals("")) {
				strSQL += " WHERE [DepCode] LIKE '" + FFUserDefOID + "' ";
			}

			Cursor cursor = db.rawQuery(strSQL, null);

			if (cursor != null) {
				if (cursor.moveToFirst()) {
					RetrunName = cursor.getString(2);
				}
			}
			cursor.close();
			db.close();
			return RetrunName;

		} catch (Exception e) {
			return null;
		}
	}
	
    public void Define_FFUser() {
		rs = null;
		
		myDb = new SQLiteDBClass(getContext());
		db = myDb.getWritableDatabase();

		try {
			rs = selectUser("", "");
			while (rs.next()) {
				ContentValues Val = new ContentValues();
				Val.put("FFUserOID", rs.getString("OID"));
				Val.put("LoginName", rs.getString("LoginName"));
				Val.put("UserName", rs.getString("UserName"));
				Val.put("Password", rs.getString("Password"));
				Val.put("DepCode", rs.getString("DepCode"));
				Val.put("DepName", rs.getString("DepName"));
				Val.put("UserGroupCode", rs.getString("UserGroupCode"));
				Val.put("UserGroupName", rs.getString("UserGroupName"));
				Val.put("PicPath", rs.getString("PicPath"));
				db.insert(TABLE_FFUSER, null, Val);
			}
			rs.close();
		} catch (Exception e) {
			Log.d("TABLE FFUSER", "Create Table not Successfully.");
		}
		Log.d("TABLE FFUSER", "Create Table Successfully.");
		db.close();
	}
    

	public String Select_FFUserDefName(String FFUserDefOID) throws Exception{
		// TODO Auto-generated method stub
		try {
			String RetrunName = "";

			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			String strSQL = "SELECT  FFUserOID As _id , * FROM " + TABLE_FFUSER
					+ " WHERE [DepCode] LIKE '" + FFUserDefOID + "' ";
			Cursor cursor = db.rawQuery(strSQL, null);

			if (cursor != null) {
				if (cursor.moveToFirst()) {
					RetrunName = cursor.getString(6);
				}
			}
			cursor.close();
			db.close();
			return RetrunName;

		} catch (Exception e) {
			return null;
		}
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

   
}
